import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    //队列实现栈
    public Queue<Integer> que1;
    public Queue<Integer> que2;
    public MyStack() {
        que1 = new LinkedList<>();
        que2 = new LinkedList<>();
    }

    public void push(int x) {

        if(!que1.isEmpty()){
            que1.offer(x);
        }else if(!que2.isEmpty()){
            que2.offer(x);
        }else{
            que1.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!que1.isEmpty()){
            int x = que1.size();
            while ( x-1 >0 ){
                que2.offer(que1.poll());
                x--;
            }
            return que1.poll();
        }else if(!que2.isEmpty()){
            int x = que2.size();
            while ( x-1 >0 ){
                que1.offer(que2.poll());
                x--;
            }
            return que2.poll();
        }
        return -1;
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(!que1.isEmpty()){
            int x = que1.size();
            while ( x-1 >0 ){
                que2.offer(que1.poll());
                x--;
            }
            return que1.peek();
        }else if(!que2.isEmpty()){
            int x = que2.size();
            while ( x-1 >0 ){
                que1.offer(que2.poll());
                x--;
            }
            return que2.peek();
        }
        return -1;
    }

    public boolean empty() {
        return que1.isEmpty()&&que2.isEmpty();
    }
}
